package com.zaicheng.cloud.loadbanlancer.server;
/**
 @author zaicheng
 @version 1.0
 @date 2022-04-19
 @email zzc15847256473@163.com
 */

import com.zaicheng.cloud.loadbanlancer.util.Pair;

/**
 * 服务实例对象，主要包含获取到的服务信息：例如：主机，ip,端口号，等等
 * @author 张在成
 */
public class Server {

    private String host;
    private int port;
    private String scheme;
    private volatile String id;

    private volatile boolean isAliveFlag;
    private String zone;
    private volatile boolean readyToServe;

    public Server() {
    }

    public Server(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public Server(String host, int port, String scheme) {
        this.host = host;
        this.port = port;
        this.scheme = scheme;
    }

    private static String getScheme(String id) {
        if (id != null) {
            if (id.toLowerCase().startsWith("http://")) {
                return "http";
            }

            if (id.toLowerCase().startsWith("https://")) {
                return "https";
            }
        }

        return null;
    }

    public void setAlive(boolean isAliveFlag) {
        this.isAliveFlag = isAliveFlag;
    }

    public boolean isAlive() {
        return this.isAliveFlag;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        if (host != null) {
            this.host = host;
            this.id = host + ":" + this.port;
        }

    }


    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
        if (this.host != null) {
            this.id = this.host + ":" + port;
        }

    }

    public String getScheme() {
        return scheme;
    }

    public void setScheme(String scheme) {
        this.scheme = scheme;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        Pair<String, Integer> hostPort = getHostPort(id);
        if (hostPort != null) {
            this.id = (String)hostPort.first() + ":" + hostPort.second();
            this.host = (String)hostPort.first();
            this.port = (Integer)hostPort.second();
            this.scheme = getScheme(id);
        } else {
            this.id = null;
        }

    }

    static Pair<String, Integer> getHostPort(String id) {
        if (id != null) {
            String host = null;
            int port = 80;
            if (id.toLowerCase().startsWith("http://")) {
                id = id.substring(7);
                port = 80;
            } else if (id.toLowerCase().startsWith("https://")) {
                id = id.substring(8);
                port = 443;
            }

            int colon_idx;
            if (id.contains("/")) {
                colon_idx = id.indexOf("/");
                id = id.substring(0, colon_idx);
            }

            colon_idx = id.indexOf(58);
            if (colon_idx == -1) {
                host = id;
            } else {
                host = id.substring(0, colon_idx);

                try {
                    port = Integer.parseInt(id.substring(colon_idx + 1));
                } catch (NumberFormatException var5) {
                    throw var5;
                }
            }

            return new Pair(host, port);
        } else {
            return null;
        }
    }


    public boolean isAliveFlag() {
        return isAliveFlag;
    }

    public void setAliveFlag(boolean aliveFlag) {
        isAliveFlag = aliveFlag;
    }

    public String getZone() {
        return zone;
    }

    public void setZone(String zone) {
        this.zone = zone;
    }

    public boolean isReadyToServe() {
        return readyToServe;
    }

    public void setReadyToServe(boolean readyToServe) {
        this.readyToServe = readyToServe;
    }

    public interface MetaInfo {
        String getAppName();

        String getServerGroup();

        String getServiceIdForDiscovery();

        String getInstanceId();
    }

}
